package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;
import org.openqa.selenium.Keys;
import org.openqa.selenium.WebElement;
import org.openqa.selenium.interactions.Actions;
import org.openqa.selenium.support.ui.ExpectedConditions;
import org.openqa.selenium.support.ui.WebDriverWait;

import java.io.IOException;
import java.time.Duration;

public class LoginPage extends Utils {
    public static String url = "http://8.155.1.153:8081/user/login";
    public LoginPage() {
        super(url);
    }
    //查看页面关键元素
    public void checkPageRight() throws IOException {
        //查看页面关键元素
        //检查标题，小标题是否加载成功
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.top___1C1Zi > div.header___5xZ3f > span"));
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.top___1C1Zi > div.desc___-njyT"));
        //检查注册框是否加载成功
        driver.findElement(By.cssSelector("#username"));
        driver.findElement(By.cssSelector("#password"));
        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button"));


        //方法截图
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    //能否正常登录
    /*登录失败
    账号为空，密码为空，点击登录按钮
    账号为空，密码正确，点击登录按钮
    账号错误，密码为空，点击登录按钮
    账号错误，密码正确，点击登录按钮
    账号正确，密码错误，点击登录按钮
    账号为空，密码正确，点击登录按钮
    */
    //账号为空，密码为空
    public void LoginFail() throws IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("");
        driver.findElement(By.cssSelector("#password")).sendKeys("");

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        String actualMsg=driver. findElement(By.cssSelector("#username_help > div")).getText();
        String expectMsg = "用户名是必填项！";
        assert actualMsg.equals(expectMsg);

        String actualMsg2=driver.findElement(By.cssSelector("#password_help > div")).getText();
        String expectMsg2 = "密码是必填项！";
        assert actualMsg2.equals(expectMsg2);

        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    //账号为空，密码正确
    public void LoginFail1() throws IOException {
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#username_help > div")).getText();
        String expectMsg = "用户名是必填项！";
        assert actualMsg.equals(expectMsg);

        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    //账号错误，密码为空
    public void LoginFail2() throws IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(3));

        WebElement ele=driver.findElement(By.cssSelector("#password"));
        Actions action=new Actions(driver);
        action.doubleClick(ele)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        driver.findElement(By.cssSelector("#username")).sendKeys("GGBond");
        driver.findElement(By.cssSelector("#password")).sendKeys("");

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#password_help > div")).getText();
        String expectMsg = "密码是必填项！";
        assert actualMsg.equals(expectMsg);

        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    //账号错误，密码正确
    public void LoginFail3() throws IOException {
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")).getText();
        String expectMsg = "错误的用户名和密码";
        assert actualMsg.equals(expectMsg);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }
    //账号正确，密码错误
    public void LoginFail4() throws IOException {
        driver.manage().timeouts().implicitlyWait(Duration.ofSeconds(5));

        WebElement ele=driver.findElement(By.cssSelector("#username"));
        Actions action=new Actions(driver);
        action.doubleClick(ele)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        driver.findElement(By.cssSelector("#username")).sendKeys("LiLi");
        driver.findElement(By.cssSelector("#password")).sendKeys("111111");

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();

        String actualMsg=driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > div.ant-alert.ant-alert-error > div > div")).getText();
        String expectMsg = "错误的用户名和密码";
        assert actualMsg.equals(expectMsg);
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

    //登录成功
    public void LoginSuc() throws IOException, InterruptedException {
        WebElement ele=driver.findElement(By.cssSelector("#password"));
        Actions action=new Actions(driver);
        action.doubleClick(ele)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果
        WebElement ele1=driver.findElement(By.cssSelector("#username"));
        Actions action1=new Actions(driver);
        action.doubleClick(ele1)
                .sendKeys(Keys.DELETE)//使用键盘的delete键
                .perform();//演示效果

        driver.findElement(By.cssSelector("#username")).sendKeys("LiLi");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");

        driver.findElement(By.cssSelector("#root > div > div.content___2zk1- > div.main___x4OjT > div > form > button")).click();
        wait.until(ExpectedConditions.visibilityOfElementLocated(By.xpath("//*[@id=\"sk-layout\"]/div/div/section/div[2]/header[2]/div/div[1]/a/h1")));
        ScreenShot(Thread.currentThread().getStackTrace()[1].getMethodName());
    }

}
